package com.css.fxfzypg.modules.baseData.service.impl;

import cn.afterturn.easypoi.excel.entity.ImportParams;
import com.css.fxfzypg.constants.FxfzConstants;
import com.css.fxfzypg.constants.YNEnum;
import com.css.fxfzypg.modules.baseData.queryParams.KeyProjectQueryParams;
import com.css.fxfzypg.modules.baseData.repository.KeyProjectRepository;
import com.css.fxfzypg.modules.baseData.entity.KeyProjectEntity;
import com.css.fxfzypg.modules.baseData.service.*;
import com.css.fxfzypg.util.FileUtil;
import com.css.fxfzypg.util.UUIDGenerator;
import com.css.fxfzypg.web.PlatformSessionContext;
import com.css.fxfzypg.zcpt.sys.service.SysAreaService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.net.URLEncoder;
import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * @author y1fan
 * @create 2022-08-24 10:32
 */
@Slf4j
@Service
public class KeyProjectServiceImpl implements KeyProjectService {

    @Resource
    private KeyProjectRepository keyProjectRepository;

    @Autowired
    private SysAreaService sysAreaService;

    @Autowired
    private NucProjectService nucProjectService;
    @Autowired
    private WatPowProjectService watPowProjectService;
    @Autowired
    private PetIndProjectService petIndProjectService;
    @Autowired
    private TrarPojectService traProjectService;
    @Autowired
    private SciExpProjectService sciExpProjectService;
    @Autowired
    private OthProjectService othProjectService;



    public void saveKeyProject(KeyProjectEntity keyProjectEntity) {
        keyProjectEntity.setDkpCreateUser(PlatformSessionContext.getUserID());
        keyProjectEntity.setDkpCreateTime(new Date());
        keyProjectEntity.setDkpIsDelete(YNEnum.N.toString());
        keyProjectEntity.setDkpId(UUIDGenerator.getUUID());
        String province = sysAreaService.getAreaNameByAreaId(keyProjectEntity.getDkpProvinceId());
        String city = sysAreaService.getAreaNameByAreaId(keyProjectEntity.getDkpCityId());
        String prefecture = sysAreaService.getAreaNameByAreaId(keyProjectEntity.getDkpPrefectureId());
        keyProjectEntity.setDkpProvince(province);
        keyProjectEntity.setDkpCity(city);
        keyProjectEntity.setDkpPrefecture(prefecture);
        keyProjectRepository.saveKeyProject(keyProjectEntity);
    }

    @Override
    public KeyProjectEntity getKeyProjectById(String id) {
        return keyProjectRepository.getKeyProjectById(id);
    }

    @Override
    public void updateKeyProject(KeyProjectEntity keyProjectEntity) {
        keyProjectEntity.setDkpUpdateTime(new Date());
        keyProjectEntity.setDkpUpdateUser(PlatformSessionContext.getUserID());
        String province = sysAreaService.getAreaNameByAreaId(keyProjectEntity.getDkpProvinceId());
        String city = sysAreaService.getAreaNameByAreaId(keyProjectEntity.getDkpCityId());
        String prefecture = sysAreaService.getAreaNameByAreaId(keyProjectEntity.getDkpPrefectureId());
        keyProjectEntity.setDkpProvince(province);
        keyProjectEntity.setDkpCity(city);
        keyProjectEntity.setDkpPrefecture(prefecture);
        String dkpKeyType = keyProjectEntity.getDkpKeyType();
        if (FxfzConstants.NUCLEUS_PROJECT.equals(dkpKeyType)) {
            nucProjectService.update(keyProjectEntity);
        }
        if (FxfzConstants.WATER_POWER_PROJECT.equals(dkpKeyType)) {
            watPowProjectService.update(keyProjectEntity);
        }
        if (FxfzConstants.PETROCHEMICAL_INDUSTRY_PROJECT.equals(dkpKeyType)) {
            petIndProjectService.update(keyProjectEntity);
        }
        if (FxfzConstants.TRANSPORTATION_PROJECT.equals(dkpKeyType)) {
            traProjectService.update(keyProjectEntity);
        }
        if (FxfzConstants.SCIENTIFIC_EXPERIMENT_PROJECT.equals(dkpKeyType)) {
            sciExpProjectService.update(keyProjectEntity);
        }
        if (FxfzConstants.OTHER_PROJECT.equals(dkpKeyType)) {
            othProjectService.update(keyProjectEntity);
        }
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void deleteKeyProject(List<String> ids) {
        keyProjectRepository.deleteKeyProject(ids);
    }

    @Override
    public Map<String, Object> getAllKeyProjectByParams(KeyProjectQueryParams queryParams, int curPage, int pageSize) {
        Map<String,Object> page=keyProjectRepository.getAllKeyProjectByParams(queryParams,curPage,pageSize);
        return page;
    }

    @Override
    public void exportExcel(HttpServletResponse response, KeyProjectQueryParams queryParams) {
        List<String> idList = keyProjectRepository.findFormDataListByDkpKeyType(queryParams);
        if (idList == null || idList.size() <= 0) {
            throw new RuntimeException("数据为空,无法导出！");
        }
        String dkpKeyType = queryParams.getDkpKeyType();
        if (FxfzConstants.NUCLEUS_PROJECT.equals(dkpKeyType)) {
            nucProjectService.exportExcel(idList,response);
        }
        if (FxfzConstants.WATER_POWER_PROJECT.equals(dkpKeyType)) {
            watPowProjectService.exportExcel(idList,response);
        }
        if (FxfzConstants.PETROCHEMICAL_INDUSTRY_PROJECT.equals(dkpKeyType)) {
           petIndProjectService.exportExcel(idList,response);
        }
        if (FxfzConstants.TRANSPORTATION_PROJECT.equals(dkpKeyType)) {
            traProjectService.exportExcel(idList,response);
        }
        if (FxfzConstants.SCIENTIFIC_EXPERIMENT_PROJECT.equals(dkpKeyType)) {
            sciExpProjectService.exportExcel(idList,response);
        }
        if (FxfzConstants.OTHER_PROJECT.equals(dkpKeyType)) {
            othProjectService.exportExcel(idList,response);
        }
    }


    @Override
    public String importExcel(MultipartFile file, String dkpKeyType) {
        try {
            // 返回的消息
            StringBuilder returnMsg = new StringBuilder();
            // 导入的参数信息
            ImportParams params = new ImportParams();
            // 设置简析的第一行
            params.setTitleRows(0);
            params.setHeadRows(1);
            // 是否需要校验
            params.setNeedVerify(true);
            // 获取到Excel数据
            if (FxfzConstants.NUCLEUS_PROJECT.equals(dkpKeyType)) {
                String nucMsg=nucProjectService.importNucProject(file,params,dkpKeyType);
                returnMsg.append(nucMsg);
            }else if (FxfzConstants.WATER_POWER_PROJECT.equals(dkpKeyType)) {
                String watPowMsg=watPowProjectService.importWatPowProject(file,params,dkpKeyType);
                returnMsg.append(watPowMsg);
            }else if (FxfzConstants.PETROCHEMICAL_INDUSTRY_PROJECT.equals(dkpKeyType)) {
                String petIndMsg=petIndProjectService.importPetIndProject(file,params,dkpKeyType);
                returnMsg.append(petIndMsg);
            }else if (FxfzConstants.TRANSPORTATION_PROJECT.equals(dkpKeyType)) {
                String traMsg=traProjectService.importTraProject(file,params,dkpKeyType);
                returnMsg.append(traMsg);
            }else if (FxfzConstants.SCIENTIFIC_EXPERIMENT_PROJECT.equals(dkpKeyType)) {
                String sciExpMsg=sciExpProjectService.importSciExpProject(file,params,dkpKeyType);
                returnMsg.append(sciExpMsg);
            }else{
                String othcMsg=othProjectService.importOthProject(file,params,dkpKeyType);
                returnMsg.append(othcMsg);
            }
            return returnMsg.toString();
        } catch (Exception e) {
            log.error(e.getMessage(), e);
            return "导入失败，请检查数据正确性";
        }
    }

    @Override
    public Integer countByCounty(String province, String city, String county) {
        return keyProjectRepository.countByCounty(province, city, county);
    }

    @Override
    public Integer countByCity(String province, String city) {
        return keyProjectRepository.countByCity(province, city);
    }

    @Override
    public Integer countByProvince(String name) {
        return keyProjectRepository.countByProvince(name);
    }

    @Override
    public Integer countByProvinceAndYear(String provinceName, String taskYear) {
        return keyProjectRepository.countByProvinceAndYear(provinceName, taskYear);
    }
}
